<template>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="700px" append-to-body>
        <el-form ref="form" :model="model" :rules="rules" inline label-width="170px" label-suffix=":" class="form-class" v-loading="loading">
            <el-form-item label="月份" prop="date">
                <el-date-picker type="month" v-model="model.date" placeholder="请选择月份" clearable></el-date-picker>
            </el-form-item>
            <el-form-item label="SPU" prop="spu">
                <el-input v-model.trim="model.spu" placeholder="请输入SPU" clearable></el-input>
            </el-form-item>
            <el-form-item label="单价（RMB）" prop="material">
                <el-input v-model.trim="model.material" placeholder="请输入单价（RMB）" clearable></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" :loading="save" :disabled="save" @click="submit">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
import { queryOrderCheckSpu, editOrderCheckSpu } from '@/api/order'
import { parseTime } from '@/utils/quickadmin'
export default {
    name: 'DetailDialog',
    props: {
        value: {
            type: Boolean,
            default: false,
            required: true,
        },
        params: {
            type: Object,
            default: () => {},
        },
    },
    data() {
        return {
            loading: false,
            save: false,
            model: {
                id: '',
                date: '',
                spu: '',
                material: '',
            },
            rules: {
                date: [{ required: true, message: '月份不能为空', trigger: ['blur', 'change'] }],
                spu: [{ required: true, message: 'SPU不能为空', trigger: ['blur', 'change'] }],
                material: [{ required: true, message: '采购成本不能为空', trigger: ['blur', 'change'] }],
            },
        }
    },
    computed: {
        title() {
            return `${this.model.id ? '编辑' : '新增'}采购成本-SPU`
        },
        dialogVisible: {
            get() {
                return this.value
            },
            set(val) {
                this.$emit('input', val)
            },
        },
    },
    watch: {
        params: {
            handler(val) {
                if (val.id) {
                    this.getDetail()
                }
            },
            immediate: true,
            deep: true,
        },
    },
    mounted() {
        // this.getDetail()
    },
    methods: {
        getDetail() {
            this.loading = true
            const params = { id: this.params.id }
            queryOrderCheckSpu(params)
                .then(res => {
                    this.model = Object.assign({}, this.model, res.data)
                    this.model.date = this.model.date ? new Date(this.model.date) : undefined
                })
                .finally(() => {
                    this.loading = false
                })
        },
        submit() {
            this.$refs.form.validate(valid => {
                if (!valid) {
                    return
                }

                const params = { 
                    ...this.model,
                    date: parseTime(this.model.date, 'y-m')
                 }
                if (this.save) {
                    return
                }
                this.save = true
                editOrderCheckSpu(params)
                    .then(res => {
                        this.$message.success(res.msg)
                        this.dialogVisible = false
                        this.$emit('success')
                    })
                    .finally(() => {
                        this.save = false
                    })
            })
        },
        cancel() {
            this.dialogVisible = false
        },
    },
}
</script>

<style>
.form-class.el-form .el-form-item {
    width: 100%;
    margin-right: 0;
}
.form-class.el-form .el-form-item .el-input {
    width: 400px;
}
</style>
